package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Role;
import com.example.demo.entity.RoleMenu;
import com.example.demo.entity.UserRole;
import com.example.demo.entity.dto.GiveMenuDto;
import com.example.demo.entity.dto.GiveRolesDto;
import com.example.demo.entity.dto.PageDto;
import com.example.demo.entity.vo.PageVo;
import com.example.demo.enums.StatusCodeEnum;
import com.example.demo.exception.CustomException;
import com.example.demo.service.RoleMenuService;
import com.example.demo.service.RoleService;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.util.NormalUtil;
import com.example.demo.util.QueryWraperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【role】的数据库操作Service实现
* @createDate 2024-01-03 17:28:05
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>  implements RoleService{

    @Autowired
    RoleMenuService roleMenuService;

    @Override
    public List<Role> getList(Role role) {
        return baseMapper.selectList(QueryWraperUtil.getQueryWrap(role));
    }

    @Override
    public PageVo<Role> getPage(PageDto<Role> pageDto) {
        IPage<Role> iPage=baseMapper.selectPage(new Page<>(pageDto.getCurrent(),pageDto.getSize()),QueryWraperUtil.getQueryWrap(pageDto.getCdt()));
        return new PageVo<>(iPage.getRecords(),iPage.getCurrent(),iPage.getSize(),iPage.getTotal());
    }

    @Override
    @Transactional(rollbackFor = CustomException.class)
    public Boolean giveMenus(GiveMenuDto giveMenuDto) {
        Integer delNum;
        boolean saveSuccess;
        giveMenuDto.getIdList().forEach(v->{v.setRid(giveMenuDto.getRid());});
        try{
            delNum=roleMenuService.deleteByRoleId(giveMenuDto.getRid());
            saveSuccess=roleMenuService.saveBatch(giveMenuDto.getIdList());
        }catch(Exception e){
            throw new CustomException(StatusCodeEnum.give_fail);
        }
        return delNum>0 && saveSuccess;
    }
}




